package com.atguigu.kafka.consumer;

import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.TopicPartition;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;

/**
 *  消费者 - offset提交
 *
 *   1. 自动提交
 *       enable.auto.commit = true  - 开启自动提交
 *       auto.commit.interval.ms = 1000  - 自动提交offset的间隔
 *
 *   2. 手动提交
 *      enable.auto.commit = false - 关闭自动提交
 *
 *      同步提交: commitSync    消费一次提交一次，
 *
 *      异步提交: commitAsync   消费跟提交是异步进行的。 提交不影响消费。
 *
 *
 *
 */
public class ConsumerDemo4 {
    public static void main(String[] args) {
        //1. 创建消费者对象
        Properties properties = new Properties();
        // 指定集群的位置
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "hadoop102:9092,hadoop103:9092,hadoop104:9092");
        // 消费者组
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, "g7");
        // 自动提交offset
        properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
        // 自动提交offset的间隔
        properties.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, 1000);

        // K 和  V的反序列化器
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");

        //设置offset重置
        properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(properties);

        //2. 调用方法消费数据
        //订阅主题
        ArrayList<String> topics = new ArrayList<>();
        topics.add("first");
        //topics.add("second");
        //topics.add("third");
        consumer.subscribe(topics);

        //持续消费
        while(true){
            ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(2));

            for (ConsumerRecord<String, String> record : records) {
                System.out.println("消费到: " + record.topic() + " : "
                        + record.partition() + " : "
                        + record.offset() + " : "
                        + record.key() + " : "
                        + record.value());

            }
            // 提交Offset
            //System.out.println("同步提交offset");
            //consumer.commitSync();  // 阻塞提交
            System.out.println("异步提交offset");
            consumer.commitAsync(new OffsetCommitCallback() {
                // 当提交完成后调用, 提交失败或者提交成功都叫完成
                @Override
                public void onComplete(Map<TopicPartition, OffsetAndMetadata> offsets, Exception exception) {
                    if(exception != null ){
                        System.out.println("提交失败: " + exception.getMessage());
                    }else{
                        System.out.println("提交成功: "  + offsets);
                    }
                }
            });

        }
        //3. 关闭消费者对象
        //consumer.close();
    }
}
